package com.Shoping.controller;

import com.Shoping.entity.Commodity;
import com.Shoping.entity.Commoditys;
import com.Shoping.util.DbUtils;
import com.alibaba.fastjson.JSON;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 前端已经实现，后端只需将选中的商品保存到目标商品块中即可
 */
@WebServlet("/shopping.do")
public class ShoppingController extends HttpServlet {

    Connection connection = DbUtils.getConnection();
    ResultSet rs;
    PreparedStatement ps;
    private int cnt = 0;

    /**
     * 向购物车中添加商品
     *
     * @return
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("shangcheng");
        Integer uId = Integer.valueOf(req.getParameter("uId"));//获取到当前的用户id
        Integer cmId = Integer.valueOf(req.getParameter("cmId"));//获取当前选中的商品id
        Integer cmsNumber = Integer.valueOf(req.getParameter("cmsNumber"));//获取添加的购物车的商品数量
        Integer cId = Integer.valueOf(req.getParameter("cId"));//获取商品的颜色id
        Integer sid = 0;//购物车id
        Integer cmPrice = 0;//商品单价
        Integer oldNumber = 0;//原来的颜色库存
        String cName = "";//颜色名称
        /**
         * 对uid进行判断，看是不是空值，若不是空值，则可以进行查找和购物车的商品插入
         */

        if (uId == null) {
            /**
             * 此处要重定向到登录页
             */
            resp.sendRedirect("/ProgramJSP/register.jsp");

        } else {

            try {
                ps = connection.prepareStatement("select s_id from shopping where u_id=" + uId);
                rs = ps.executeQuery();

                if (rs.next()) {
                    sid = rs.getInt("s_id");
                } else {
                    System.out.println("ShoppingController------------------没有找到该购物车ID!");
                }
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("ShoppingController------------------根据用户id查购物车id错误!");
            } finally {
                try {
                    ps.close();
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    System.out.println("ShoppingController------------------关闭错误!");
                }
            }

            try {
                ps = connection.prepareStatement("select cm_price from commodity where cm_id=" + cmId);
                rs = ps.executeQuery();
                if (rs.next()) {
                    cmPrice = rs.getInt("cm_price");
                } else {
                    System.out.println("ShoppingController------------------没有找到该商品价格!");
                }
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("ShoppingController------------------获取当前商品价格错误!");
            } finally {
                try {
                    ps.close();
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    System.out.println("ShoppingController------------------关闭错误!");
                }
            }


            try {
                ps = connection.prepareStatement("select c_number,c_name from color where c_id=" + cId);
                rs = ps.executeQuery();
                if (rs.next()) {
                    oldNumber = rs.getInt("c_number");
                    cName = rs.getString("c_name");
                }
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("ShoppingController------------------获取颜色库存错误!");
            } finally {
                try {
                    ps.close();
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    System.out.println("ShoppingController------------------关闭错误!");
                }
            }

            try {
                ps = connection.prepareStatement("insert into commoditys(cms_id,cm_id,s_id,cms_number,cms_subtotal,cms_colorName) values(?,?,?,?,?,?)");
                ps.setInt(1, cnt);
//                ps.setString(2,"x"+cnt++);
                ps.setInt(2, cmId);
                ps.setInt(3, sid);
                ps.setInt(4, cmsNumber);
                ps.setInt(5, cmPrice * cmsNumber);
                ps.setString(6, cName);
                ps.executeUpdate();

            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("ShoppingController------------------保存至商品块错误!");
            } finally {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    System.out.println("ShoppingController------------------关闭错误!");
                }
            }

            try {
                ps = connection.prepareStatement("update color set c_number=" + (oldNumber - cmsNumber) + " where c_id=" + cId);
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("ShoppingController------------------更新颜色库存错误!");
            } finally {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    System.out.println("ShoppingController------------------关闭错误!");
                }
            }


            //--------------------------注意----------------------------------------
            /**
             * 跳转到购物车页
             */

            Map<String, Object> map = new HashMap<>();
            map.put("flag", true);
            String s = JSON.toJSONString(map);
            System.out.println(s);
            resp.getWriter().write(s);
            //--------------------------注意----------------------------------------
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}